package com.example.designsoft.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.designsoft.R;
import com.example.designsoft.adapter.BottomAdapter;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author : Gyk
 * time   : 2021/3/19
 * todo   :
 */
public class BottomView extends RelativeLayout implements BottomNavigationView.OnNavigationItemSelectedListener {


    @BindView(R.id.rl_bottom)
    RelativeLayout mRlBottom;
    @BindView(R.id.ll_bottom)
    LinearLayout mLlBottom;
    @BindView(R.id.flAll)
    FrameLayout mFlAll;
    @BindView(R.id.BottomBnv)
    BottomNavigationView mBottomNavigationView;
    @BindView(R.id.recy_bottom)
    RecyclerView mRecyBottom;

    private Context mContext;
    private boolean isTouch = false;
    private boolean isPull = false;
    private BottomAdapter mBottomAdapter;
    private final String TAG = "BottomView";
    private List<Integer> mList;

    public BottomView(Context context) {
        this(context, null);
        this.mContext = context;
        initView();
    }

    public BottomView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        this.mContext = context;
        initView();
    }

    public BottomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.bottom_view, this);
        ButterKnife.bind(view);
        mBottomNavigationView.setOnNavigationItemSelectedListener(this);
        setLayoutY(mLlBottom, 2050);
        initRecy();
    }

    private void initRecy() {
        mList = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            mList.add(R.mipmap.linkman_select);
        }
        mBottomAdapter = new BottomAdapter(mContext, mList);
        mRecyBottom.setLayoutManager(new GridLayoutManager(mContext, 4));
        mRecyBottom.setAdapter(mBottomAdapter);
    }

    /*
     * 设置控件所在的位置Y，并且不改变宽高，
     * Y为绝对位置，此时X可能归0
     */
    private void setLayoutY(View view, int y) {
        mRlBottom.removeView(view);
        MarginLayoutParams margin = new MarginLayoutParams(view.getLayoutParams());
        margin.setMargins(0, y, 0, 0);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
        view.setLayoutParams(layoutParams);
        mFlAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e(TAG, "FLLLLL--->");
            }
        });
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.message:
                Log.e(TAG, "message--->");
                break;
            case R.id.linkman:
                Log.e(TAG, "linkman--->");
                break;
            case R.id.mine:
                Log.e(TAG, "mine--->");
                break;
        }
        return true;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.e(TAG, "Y-->" + event.getY());
                if (!isPull) {
                    if (event.getY() > 2050) {
                        isTouch = true;
                    } else {
                        isTouch = false;
                    }
                } else {
                    if (event.getY() > 1500) {
                        isTouch = true;
                    } else {
                        isTouch = false;
                    }
                }

            case MotionEvent.ACTION_MOVE:
                if (!isPull) {
                    if (isTouch) {
                        if (event.getY() < 2050 && event.getY() > 1600) {
                            setLayoutY(mLlBottom, (int) event.getY());
                            isPull = true;
                        }
                    }
                } else {
                    if (isTouch) {
                        if (event.getY() < 2050 && event.getY() > 1600) {
                            setLayoutY(mLlBottom, (int) event.getY());
                            isPull = true;
                        }
                    }
                }

                Log.e(TAG, "MOVEY-->" + event.getY());
                break;
            case MotionEvent.ACTION_UP:

                break;
        }
        return super.dispatchTouchEvent(event);

    }
}
